<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ page import="java.lang.Throwable,
				java.sql.Connection,
				java.sql.SQLException,
				java.sql.Statement,
				java.util.ArrayList,
				java.util.HashMap,
				java.util.List,
				java.util.Map,
				org.jawa.util.*,
				org.jawa.Constants,
				org.jawa.database.*,
				java.io.File"
				
%>
<%!	
    boolean testConnection(Map<String,String> errors,String driver,String serverURL,String username,String password) {
        boolean success = true;
        Connection con = null;
        try {
        	Class.forName(driver);
            con = java.sql.DriverManager.getConnection(serverURL, username, password );            
        }
        catch (SQLException ex) {
            success = false;
            errors.put("general","A connection to the database could not be "
                + "made. View the error message by opening the "
                + "\"" + File.separator + "logs" + File.separator + "error.log\" log "
                + "file, then go back to fix the problem.");

        }catch(ClassNotFoundException ex){
        	success = false;
        	 errors.put("driver","Unable to load the specified JDBC driver. Please verify the " +
                     "name of the driver is correct and that the driver is in the classpath " +
                     "of this server (usually the 'lib' directory). If you add a driver to " +
                     "your classpath you will neeed to restart the server.");
        }
        finally {
            try {
        	    con.close();
            } catch (Exception ignored) {}
        }
        return success;
    }
%>
<%  // Get parameters
    
	String mode = ParamUtils.getParameter(request,"datasourceType");//连接池类型

	String driver = ParamUtils.getParameter(request,"driver");
    String serverURL = ParamUtils.getParameter(request,"serverURL");
    String username = ParamUtils.getParameter(request,"username",true);
    String password = ParamUtils.getParameter(request,"password",true);
    int minConnections = ParamUtils.getIntParameter(request,"minConnections",-1);
    int maxConnections = ParamUtils.getIntParameter(request,"maxConnections",-1);
    double connectionTimeout = ParamUtils.getDoubleParameter(request,"connectionTimeout",0.0);
    String hdialect=ParamUtils.getParameter(request,"hdialect");
    boolean doContinue = request.getParameter("continue") != null;

    // handle a continue request
    Map<String,String> errors = new HashMap<String,String>();
    if (doContinue) {
        // Error check
        if (driver == null || "sun.jdbc.odbc.JdbcOdbcDriver".equals(driver)
                || "com.internetcds.jdbc.tds.Driver".equals(driver))
        {
            errors.put("driver","Please enter a valid JDBC driver class.");
        }
        else {
            try {
                ClassUtils.forName(driver);
            }
            catch (Throwable t) {
                errors.put("driver","Unable to load the specified JDBC driver. Please verify the " +
                        "name of the driver is correct and that the driver is in the classpath " +
                        "of this server (usually the 'lib' directory). If you add a driver to " +
                        "your classpath you will neeed to restart the server.");
            }
        }
        if (serverURL == null) {
            errors.put("serverURL", "Please enter a valid JDBC URL.");
        }
        if (minConnections < 3) {
            errors.put("minConnections","The minimum connection pool size is three connections.");
        }
        if (maxConnections < minConnections) {
            errors.put("maxConnections","The maximum number of connections cannot be less than the minimum.");
        }
        if (connectionTimeout <= 0.0) {
            errors.put("connectionTimeout","Please enter a valid connection timeout value.");
        }
		if(hdialect==null){
			errors.put("hdialect","if the dialect is null,the table can't be auto created.");
		}
        // if there were no errors, continue
        if (errors.size() == 0) {
        	mode = mode.trim();
            if(mode.equals("proxool")){
            	try {
                    
                    Constants.setXMLProperty("database.proxool.driverClassName", driver);
                    Constants.setXMLProperty("database.proxool.serverURL", serverURL);
                    Constants.setXMLProperty("database.proxool.username", username);
                    Constants.setXMLProperty("database.proxool.password", password);
                    
                    Constants.setXMLProperty("database.proxool.minConnections",
                            Integer.toString(minConnections));
                    Constants.setXMLProperty("database.proxool.maxConnections",
                            Integer.toString(maxConnections));
                    Constants.setXMLProperty("database.proxool.connectionTimeout",
                    Double.toString(connectionTimeout));
                    Constants.setXMLProperty("hibernate.dialect",hdialect);
                    if (testConnection(errors,driver,serverURL,username,password)) {
                        // Success, move on
                        response.sendRedirect("setup-mail-settings.jsp");
                        return;
                    }
                }
                catch (Exception e) {
                    errors.put("general","Setting connection properties failed - please see the error "
                            + "log located in home/logs for more details.");                
                }
            	
            }else if(mode.equals("dbcp")){

            	try {
                    Constants.setXMLProperty("database.defaultProvider.driverClassName", driver);
                    Constants.setXMLProperty("database.defaultProvider.serverURL", serverURL);
                    Constants.setXMLProperty("database.defaultProvider.username", username);
                    Constants.setXMLProperty("database.defaultProvider.password", password);
                    
                    Constants.setXMLProperty("database.defaultProvider.minConnections",
                            Integer.toString(minConnections));
                    Constants.setXMLProperty("database.defaultProvider.maxConnections",
                            Integer.toString(maxConnections));
                    Constants.setXMLProperty("database.defaultProvider.connectionTimeout",
                    Double.toString(connectionTimeout));
                    Constants.setXMLProperty("hibernate.dialect",hdialect);
                    if (testConnection(errors,driver,serverURL,username,password)) {
                        // Success, move on
                        response.sendRedirect("setup-mail-settings.jsp");
                        return;
                    }
                }
                catch (Exception e) {
                    errors.put("general","Setting connection properties failed - please see the error "
                            + "log located in home/logs for more details.");                
                }
            }
            
            // No errors setting the properties, so test the connection
          //  DbConnectionManager.setConnectionProvider(conProvider);
        }        
    }

    if (!doContinue) {
        // reset values of jdbc driver from props file
        try{
        	if(mode.equals("proxool")){
        		driver = Constants.getXMLProperty("database.proxool.driverClassName");
		        serverURL = Constants.getXMLProperty("database.proxool.serverURL");
		        username = Constants.getXMLProperty("database.proxool.username");
		        password = Constants.getXMLProperty("database.proxool.password");
        	}else if(mode.equals("dbcp")){
		        driver = Constants.getXMLProperty("database.defaultProvider.driverClassName");
		        serverURL = Constants.getXMLProperty("database.defaultProvider.serverURL");
		        username = Constants.getXMLProperty("database.defaultProvider.username");
		        password = Constants.getXMLProperty("database.defaultProvider.password");
        	}
        	hdialect=Constants.getXMLProperty("hibernate.dialect");
        }catch(Exception e){}
        try {
            minConnections = Integer.parseInt(
            		Constants.getXMLProperty("database.defaultProvider.minConnections"));
        }
        catch (Exception e) {
            minConnections = 5;
        }
        try {
            maxConnections = Integer.parseInt(
            		Constants.getXMLProperty("database.defaultProvider.maxConnections"));
        }
        catch (Exception e) {
            maxConnections = 25;
        }
        try {
            connectionTimeout = Double.parseDouble(
            		Constants.getXMLProperty("database.defaultProvider.connectionTimeout"));
        }
        catch (Exception e) {
            connectionTimeout = 1.0;
        }
    }
%>

<html>
<head>
    <title><fmt:message key="setup.datasource.standard.title" /></title>
    <meta name="currentStep" content="3"/>
</head>
<body>

	<h1>
	<fmt:message key="setup.datasource.standard.title" />
	</h1>

	<p>
	<fmt:message key="setup.datasource.standard.info" />
	</p>

	<p>
	<b><fmt:message key="setup.datasource.standard.info2" /> </b>
	</p>

	<%  if (errors.size() > 0) { %>
	
	    <p class="error">
	    <%= errors.get("general") %>
	    </p>
	
	<%  } %>

	<!-- BEGIN jawa-contentBox -->
	<div class="jawa-contentBox">
<%  // DB preset data
    List<String[]> presets = new ArrayList<String []>();
    presets.add(new String[]{"MySQL","com.mysql.jdbc.Driver","jdbc:mysql://[host-name]:3306/[database-name]","org.hibernate.dialect.MySQLInnoDBDialect"});
    presets.add(new String[]{"Oracle","oracle.jdbc.driver.OracleDriver","jdbc:oracle:thin:@[host-name]:1521:[SID]","org.hibernate.dialect.Oracle9Dialect"});
    presets.add(new String[]{"Microsoft SQLServer","net.sourceforge.jtds.jdbc.Driver","jdbc:jtds:sqlserver://[host-name]:3683/[database-name]","org.hibernate.dialect.SQLServerDialect"});
    presets.add(new String[]{"PostgreSQL","org.postgresql.Driver","jdbc:postgresql://[host-name]:5432/[database-name]","org.hibernate.dialect.PostgreSQLDialect"});
    presets.add(new String[]{"IBM DB2","com.ibm.db2.jcc.DB2Driver","jdbc:db2://[host]:50000/[database-name]","cirrus.hibernate.sql.DB2Dialect"});
       
%>
<script language="JavaScript" type="text/javascript">
	var data = new Array();
	<%  for (int i=0; i<presets.size(); i++) {
	        String[] data = presets.get(i);
	%>
	    data[<%= i %>] = new Array('<%= data[0] %>','<%= data[1] %>','<%= data[2] %>','<%= data[3] %>');
	<%  } %>
	function populate(i) {
	    document.dbform.driver.value=data[i][1];
	    document.dbform.serverURL.value=data[i][2];
	    document.dbform.hdialect.value=data[i][3];
	}
	var submitted = false;
	function checkSubmit() {
	    if (!submitted) {
	        submitted = true;
	        return true;
	    }
	    return false;
	}
</script>

<form action="setup-datasource-standard-settings.jsp" method="post" name="dbform" onsubmit="return checkSubmit();">
    <input type="hidden" name="datasourceType" value="<%=mode %>">
	<table cellpadding="3" cellspacing="2" border="0">
		<tr>
			<td nowrap align="right"><fmt:message key="setup.datasource.standard.label" />:</td>
		    <td>
		        <select size="1" name="presets" onchange="populate(this.options[this.selectedIndex].value)">
		            <option value=""><fmt:message key="setup.datasource.standard.pick_database" />
		            <%  for (int i=0; i<presets.size(); i++) {
		                    String[] data = presets.get(i);
		            %>
		                <option value="<%= i %>"> &#149; <%= data[0] %>
		            <%  } %>
		        </select>
		    </td>
		</tr>
		<tr valign="top">
		    <td nowrap align="right">
		        <fmt:message key="setup.datasource.standard.jdbc" />
		    </td>
		    <td>
		        <input type="text" name="driver" size="50" maxlength="150"
		         value="<%= ((driver != null) ? driver : "") %>">
		        
		    </td>
			<td>
				<span class="jawa-setup-helpicon" onmouseover="domTT_activate(this, event, 'content', '<fmt:message key="setup.datasource.standard.jdbc_info" />', 'styleClass', 'jawaTooltip', 'trail', true, 'delay', 300, 'lifetime', 8000);"></span>
					        <%  if (errors.get("driver") != null) { %>
					            <span class="jawa-error-text">
					            <%= errors.get("driver") %>
					            </span>
					        <%  } %>
			</td>
		</tr>
		<tr valign="top">
		    <td nowrap align="right">
		        <fmt:message key="setup.datasource.standard.url" />
		    </td>
		    <td>
		        <input type="text" name="serverURL" size="50" maxlength="250"
		         value="<%= ((serverURL != null) ? serverURL : "") %>">			   
		    </td>
			<td>
				 <span class="jawa-setup-helpicon" onmouseover="domTT_activate(this, event, 'content', '<fmt:message key="setup.datasource.standard.valid_url" />', 'styleClass', 'jawaTooltip', 'trail', true, 'delay', 300, 'lifetime', 8000);"></span>
		        <%  if (errors.get("serverURL") != null) { %>
		            <span class="jawa-error-text">
		            <%= errors.get("serverURL") %>
		            </span>
		        <%  } %>
			</td>
		</tr>
		<tr valign="top">
		    <td nowrap align="right">
		        <fmt:message key="setup.datasource.standard.dialect" />
		    </td>
		    <td>
		        <input type="text" name="hdialect" readonly="readonly" size="50" maxlength="250"
		         value="<%= ((hdialect != null) ? hdialect : "") %>">
		    </td>
			<td>
				 <span class="jawa-setup-helpicon" onmouseover="domTT_activate(this, event, 'content', '<fmt:message key="setup.datasource.standard.dialect_info" />', 'styleClass', 'jawaTooltip', 'trail', true, 'delay', 300, 'lifetime', 8000);"></span>
		        <%  if (errors.get("hdialect") != null) { %>
		            <span class="jawa-error-text">
		            <%= errors.get("hdialect") %>
		            </span>
		        <%  } %>
			</td>
		</tr>
		<tr><td colspan="2">&nbsp;</td></tr>
		<tr valign="top">
		    <td nowrap align="right">
		        <fmt:message key="setup.datasource.standard.username" />
		    </td>
		   <td>
		        <input type="text" name="username" size="20" maxlength="50"
		         value="<%= ((username != null) ? username : "") %>">		       
		    </td>
			<td>
				 <span class="jawa-setup-helpicon" onmouseover="domTT_activate(this, event, 'content', '<fmt:message key="setup.datasource.standard.username_info" />', 'styleClass', 'jawaTooltip', 'trail', true, 'delay', 300, 'lifetime', 8000);"></span>
		        <%  if (errors.get("username") != null) { %>
		            <span class="jawa-error-text">
		            <%= errors.get("username") %>
		            </span>
		        <%  } %>
			</td>
		</tr>
		<tr valign="top">
		    <td nowrap align="right">
		        <fmt:message key="setup.datasource.standard.password" />
		    </td>
		    <td>
		        <input type="password" name="password" size="20" maxlength="50"
		         value="<%= ((password != null) ? password : "") %>">		        
		    </td>
			<td><span class="jawa-setup-helpicon" onmouseover="domTT_activate(this, event, 'content', '<fmt:message key="setup.datasource.standard.password_info" />', 'styleClass', 'jawaTooltip', 'trail', true, 'delay', 300, 'lifetime', 8000);"></span>
		        <%  if (errors.get("password") != null) { %>
		            <span class="jawa-error-text">
		            <%= errors.get("password") %>
		            </span>
		        <%  } %>
			</td>
		</tr>
		<tr><td colspan="2">&nbsp;</td></tr>
		<tr valign="top">
		    <td nowrap align="right">
		        <%--<fmt:message key="setup.datasource.standard.connect" />--%>
		        Minimum Connections:
		    </td>
		    <td>
			    <input type="text" name="minConnections" size="5" maxlength="5" value="<%= ((minConnections != -1) ? ""+minConnections : "") %>">
		        
		    </td>
				<td><span class="jawa-setup-helpicon" onmouseover="domTT_activate(this, event, 'content', '<fmt:message key="setup.datasource.standard.pool" />', 'styleClass', 'jawaTooltip', 'trail', true, 'delay', 300, 'lifetime', 8000);"></span>
		        <%  if (errors.get("minConnections") != null) { %>
		            <span class="jawa-error-text">
		            <%= errors.get("minConnections") %>
		            </span>
		        <%  } %></td>
		</tr>
		<tr valign="top">
		    <td nowrap align="right">
		        <%--<fmt:message key="setup.datasource.standard.connect" />--%>
		        Maximum Connections:
		    </td>
		    <td>
			    <input type="text" name="maxConnections" size="5" maxlength="5" value="<%= ((maxConnections != -1) ? ""+maxConnections : "") %>">
		        
		    </td>
				<td><span class="jawa-setup-helpicon" onmouseover="domTT_activate(this, event, 'content', '<fmt:message key="setup.datasource.standard.pool" />', 'styleClass', 'jawaTooltip', 'trail', true, 'delay', 300, 'lifetime', 8000);"></span>
		        <%  if (errors.get("maxConnections") != null) { %>
		            <span class="jawa-error-text">
		            <%= errors.get("maxConnections") %>
		            </span>
		        <%  } %></td>
		</tr>
		<tr valign="top">
		    <td nowrap align="right">
		        <fmt:message key="setup.datasource.standard.timeout" />
		    </td>
		    <td>
		        <input type="text" name="connectionTimeout" size="5" maxlength="5"
		         value="<%= connectionTimeout %>">Days
		        
		    </td>
			<td>
				<span class="jawa-setup-helpicon" onmouseover="domTT_activate(this, event, 'content', '<fmt:message key="setup.datasource.standard.timeout_info" />', 'styleClass', 'jawaTooltip', 'trail', true, 'delay', 300, 'lifetime', 8000);"></span>
		        <%  if (errors.get("connectionTimeout") != null) { %>
		            <span class="jawa-error-text">
		            <%= errors.get("connectionTimeout") %>
		            </span>
		        <%  } %>
			</td>
		</tr>		
	</table>

<br>

		<div align="right"><div class="jawa-description" style="padding-bottom:10px;">
			<fmt:message key="setup.datasource.standard.note" /></div>
			<input type="Submit" name="continue" value="<fmt:message key="button.continue" />" id="jawa-setup-save" border="0">
		</div>
	</form>
	</div>
	<!-- END jawa-contentBox -->
</body>
</html>